package com.tsinghuabigdata.edu.ddmath;

import android.content.Context;
import android.os.Environment;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.SDKOptions;
import com.netease.nimlib.sdk.StatusBarNotificationConfig;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.msg.MsgService;
import com.tsinghuabigdata.edu.ddmath.activity.InitActivity;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.NeteaseAccountUtils;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.NimCache;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.inject.FlavorDependent;
import com.tsinghuabigdata.edu.ddmath.util.SystemUtil;
import com.tsinghuabigdata.edu.ddmath.util.WindowUtils;

//import com.tsinghuabigdata.edu.ddmath.receive.ShutdownReceiver;
//import com.tsinghuabigdata.edu.ddmath.bean.LoginInfo;

public class NeteaseImApplication {

    public static final String LOG_TAG = "ddapp";

    private Context mContext;

    public static void init(Context context) {
        NimCache.setContext(context);
        NIMClient.init(context, getLoginInfo(), getOptions(context));
        if (inMainProcess(context)) {
            // 注册自定义消息附件解析器
            NIMClient.getService(MsgService.class).registerCustomAttachmentParser(FlavorDependent.getInstance().getMsgAttachmentParser());
            // init tools
            NimCache.initImageLoaderKit();

            FlavorDependent.getInstance().onApplicationCreate();
        }
    }

    public static LoginInfo getLoginInfo() {
        LoginInfo loginInfo = NeteaseAccountUtils.getNeteaseLoginInfo();
        if (loginInfo != null) {
            NimCache.setAccount(loginInfo.getAccount().toLowerCase());
        }
        return loginInfo;
    }

    public static SDKOptions getOptions(Context context) {
        SDKOptions options = new SDKOptions();

        // 如果将新消息通知提醒托管给SDK完成，需要添加以下配置。
        StatusBarNotificationConfig config = NeteaseAccountUtils.getStatusbarNotifyConfig();
        if (config == null) {
            config = new StatusBarNotificationConfig();
        }
        // 点击通知需要跳转到的界面
        config.notificationEntrance = InitActivity.class;
        config.notificationSmallIconId = R.drawable.ic_launcher;

        // 通知铃声的uri字符串
        config.notificationSound = "android.resource://com.netease.nim.demo/raw/msg";
        options.statusBarNotificationConfig = config;
        NeteaseAccountUtils.setStatusbarNotifyConfig(config);

        // 配置保存图片，文件，log等数据的目录
        String sdkPath = Environment.getExternalStorageDirectory() + "/" + context.getPackageName() + "/nim/";
        options.sdkStorageRootPath = sdkPath;

        // 配置数据库加密秘钥
        options.databaseEncryptKey = "NETEASE";

        // 配置是否需要预下载附件缩略图
        options.preloadAttach = true;

        // 配置附件缩略图的尺寸大小，
        options.thumbnailSize = (int) (0.5 * WindowUtils.getScreenWidth(context));

        // 用户信息提供者
        options.userInfoProvider = null;

        // 定制通知栏提醒文案（可选，如果不定制将采用SDK默认文案）
        options.messageNotifierCustomization = null;

        return options;
    }

    public static boolean inMainProcess(Context context) {
        String packageName = context.getPackageName();
        String processName = SystemUtil.getProcessName(context);
        return packageName.equals(processName);
    }

//    private void initLog() {
//        String path = StorageUtil.getDirectoryByDirType(StorageType.TYPE_LOG);
//        LogUtil.init(path, Log.DEBUG);
//        LogUtil.i("demo", FlavorDependent.getInstance().getFlavorName() + " demo log path=" + path);
//    }
}
